<?php
namespace app\artic\controller;
use app\common\RestController;
use think\Controller;
use app\constant\api;
use think\Request;
use think\Session;
use app\artic\model\ArticleList;
use app\artic\model\UserInfo;
use app\artic\model\Centre;
class Portapi extends RestController{
 // class Portapi extends Controller{
    public function addinfo(){
    	 $code=$message=$data=api\SUCCESS;
    	 $param=request()->param();
    	 $result=$this->validate($param,'ArticVrify.create');
    	 if($result===true){
              if(!$data=ArticleList::addInfo([
                   'title'=>$param['title'],
                   'content'=>htmlspecialchars($param['content']),
                   // 'create_time'=>date('Y-m-d H:i:s',time()),
                   'create_time'=>time(),
              	])){
              	$code=api\DATA_INSERT_ERROR;
              }
    	 }else{
    	 	$code=api\FAIL;
    	 	$message=$result;
    	 }
    	return $this->setDirreturnVars($code,$message,$data);
    }

    public function deleinfo(){
    	$code=$message=$data=api\SUCCESS;
    	$param=request()->param();
        $result=$this->validate($param,'ArticVrify.dele');
        if($result===true){
              if(!$data=ArticleList::delInfo([
                   'id'=>$param['id'],
              	])){
              	 $code=api\DATA_DELETE_ERROR;
              }
    	 }else{
    	 	$code=api\FAIL;
    	 	$message=$result;
    	 }
    	return $this->setDirreturnVars($code,$message,$data);
    }

    public function updateinfo(){
        $code=$message=$data=api\SUCCESS;
        $param=request()->param();
        $result=$this->validate($param,'ArticVrify.update');
        if($result===true){
            if(!$data=ArticleList::updateInfo([
                'id'=>$param['id'],
            ],['title'=>$param['title'],'content'=>$param['content'],'update_time'=>time()])){
                $code=api\DATA_DELETE_ERROR;
            }
        }else{
            $code=api\FAIL;
            $message=$result;
        }
        return $this->setDirreturnVars($code,$message,$data);
    }

    //用户注册
    public function register(){
        $code=$message=$data=api\SUCCESS;
        $param=request()->param();
        $result=$this->validate($param,'ArticVrify.regist');
        if($result===true){
            if(!$data=UserInfo::addInfo([
                'user_name'=>$param['user_name'],
                'UserEmail'=>$param['UserEmail'],
                'UserPassward'=>$param['UserPasswd'],
            ])){
                $code=api\DATA_INSERT_ERROR;
            }
        }else{
            $code=api\FAIL;
            $message=$result;
        }
        return $this->setDirreturnVars($code,$message,$data);
    }
    //用户登录
    public function userlogin(){
        $code=$message=$data=api\SUCCESS;
        $param=request()->param();
        $result=$this->validate($param,'ArticVrify.login');
        if($result===true){
              //先判断验证码是否正确
              if(captcha_check($param['verify'])){
                $user=UserInfo::getInformation(['user_name'=>$param['user_name'],]);
                    if(!$user){
                            $code=api\USER_NOT_EXIST;
                    }else{
                        if($user['UserPassward']==$param['UserPassward']){
                            Session::set('user',$user['user_name']);
                        }else{
                            $code=api\PASS_INCORRECT;
                        }
                    }
              }else{
                $code=api\CAPTCHA_INCORRECT;
              }
        }else{
            $code=api\FAIL;
            $message=$result;
        }
        return $this->setDirreturnVars($code,$message,$data);
    }
    //用户退出
    public function out(){
        $code=$message=$data=api\SUCCESS;
        Session::delete('user');
        return $this->setDirreturnVars($code,$message,$data);
    }

    //发布评论
    public function issue(){
        $code=$message=$data=api\SUCCESS;
        $param=request()->param();
        $result=$this->validate($param,'ArticVrify.issue');
        if($result===true){
              $res=Centre::addInfo([
                  'article_id'=>$param['article_id'],
                  'username'=>$param['username'],
                  'content'=>$param['content'],
                  'create_time'=>date('Y-m-d H:i:s',time()),
                ]);
              !$res && $code=api\DATA_INSERT_ERROR;
        }else{
            $code=api\FAIL;
            $message=$result;
        }
        return $this->setDirreturnVars($code,$message,$data);
    }
    public function test(){
        echo "dddds";
    }
}